package main

import (
	"flag"
	"fmt"
	"os"
	"os/signal"
	"publiclib/config"
	"runtime"
	"runtime/pprof"
	"user-server/controller"

	log "github.com/sirupsen/logrus"
)

func init() {
	// 启动参数的配置文件
	flag.StringVar(&confFile, "c", "./etc/config/cloud.yaml", "path of service start config file")
	flag.Parse()
}

func main() {
	DoInitLibs()

	//以上初始失败会panic终止
	//启动auth服务
	log.Infof("start service...")
	cfg := config.Instance()
	go controller.StartGinServer(fmt.Sprintf(":%d", cfg.ListenPort))

	waitForSignal()
}

func waitForSignal() {
	for {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt, os.Kill)

		// Block until a signal is received.
		s := <-c
		fmt.Println("Got signal:", s)

		if s == os.Kill {
			// how can one send a 'kill' signal to process on windows?
			dumpGoRoutinesInfo()
		}
		break
	}
}

func dumpGoRoutinesInfo() {
	log.Info("current goroutine count:", runtime.NumGoroutine())
	// use DEBUG=2, to dump stack like golang dying due to an unrecovered panic.
	pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
}
